package com.example.studyplanning.controller;

import com.example.studyplanning.entity.Message;
import com.example.studyplanning.services.MessageServices;
import com.example.studyplanning.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("mes")
public class MessageController {
    @Autowired
    private MessageServices messageServices;

    //查询心得的留言内容
    @GetMapping("/list")
    public Result findAllMessage(@RequestParam("tipsID") Integer tipsID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(messageServices.findAllMessage(tipsID));
        return result;
    }

    //留言
    @PostMapping("/add")
    public Result insertMessage(@RequestBody Message message){
        Result result = new Result();
        int i = messageServices.insertMessage(message);
        if(i==1){
            result.setCode(200);
            result.setMessage("添加成功");
        }else{
            result.setCode(400);
            result.setMessage("添加失败");
        }
        return result;
    };

    //展示我的留言
    @GetMapping("/mymessage")
    public Result SelectByID(@RequestParam("userID") Integer userID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(messageServices.SelectByID(userID));
        return result;
    };

    //删除所选留言
    @GetMapping("/delete")
    public Result DelByID(@RequestParam("messageID") Integer messageID){
        Result result = new Result();
        int i = messageServices.DelByID(messageID);
        if (i==1){
            result.setCode(200);
            result.setMessage("删除成功");
        }else {
            result.setCode(400);
            result.setMessage("删除失败");
        }
        return result;
    };

    //留言管理
    @GetMapping("getlist")
    public Result SelectMessage(@RequestParam(value = "tipsID",defaultValue = "")Integer tipsID,
                                @RequestParam(value = "userID",defaultValue = "")Integer userID,
                                @RequestParam(value = "messageID",defaultValue = "")Integer messageID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(messageServices.SelectMessage(tipsID,userID,messageID));
        return result;
    }

    @GetMapping("all")
    public List<Message> CountDayMessage() {
        List<Message> list = messageServices.CountDayMessage();
        return list;
    }
}
